package com.dragon.manage.service.impl;


import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.Banner;
import com.dragon.manage.bean.News;
import com.dragon.manage.bean.NewsDiscussRela;
import com.dragon.manage.bean.NewsType;
import com.dragon.manage.dao.IBannerDAO;
import com.dragon.manage.dao.INewsDAO;
import com.dragon.manage.dao.INewsDiscussRelaDAO;
import com.dragon.manage.dao.INewsTypeDAO;
import com.dragon.manage.service.IManageNewsService;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.TimeUtil;

public class ManageNewsServiceImpl implements IManageNewsService{
	
	/**前台banner*/
	private IBannerDAO bannerDAO;
	/**新闻列表*/
	private INewsDAO newsDAO;
	/**评论列表*/
	private INewsDiscussRelaDAO newsDiscussRelaDAO;

	private INewsTypeDAO newsTypeDAO;
	
	public void setNewsTypeDAO(INewsTypeDAO newsTypeDAO) {
		this.newsTypeDAO = newsTypeDAO;
	}


	public void setNewsDAO(INewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}


	public void setBannerDAO(IBannerDAO bannerDAO) {
		this.bannerDAO = bannerDAO;
	}


	public void setNewsDiscussRelaDAO(INewsDiscussRelaDAO newsDiscussRelaDAO) {
		this.newsDiscussRelaDAO = newsDiscussRelaDAO;
	}


	@Override
	public JSONObject deleteNews(int newid) throws Exception {
		JSONObject reJsonObject = new JSONObject();
		newsDAO.deleteNewsByID(newid);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}
	
	@Override
	public JSONObject getNewsInfo(int newid) throws Exception {
		JSONObject reJsonObject = new JSONObject();
		News news = newsDAO.getNewsByID(newid);
		if(news==null){
			reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJsonObject;
		}
		reJsonObject.put("newId", news.getNewid());
		reJsonObject.put("title", news.getTitle());
		reJsonObject.put("createDate", TimeUtil.getDateFormat(news.getCreatetime()));
		reJsonObject.put("newstype", news.getNewstype());
		reJsonObject.put("content", news.getContent());
		reJsonObject.put("coveraddress", news.getCoveraddress());
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}
	
	@Override
	public JSONObject queryNewsList(int page) throws Exception {
		JSONObject reJsonObject = new JSONObject();
		List<News> list = newsDAO.getNewsList(page, StaticData.no_param);
		JSONArray jsa = new JSONArray();
		JSONObject jsTmp = null;
		for(News tmp:list){
			/*
			 * id,标题，时间，分类，图片
			 */
			jsTmp = new JSONObject();
			jsTmp.put("newId", tmp.getNewid());
			jsTmp.put("title", tmp.getTitle());
			jsTmp.put("createDate", TimeUtil.getDateFormat(tmp.getCreatetime()));
			jsTmp.put("newstype", tmp.getNewstype());
			jsTmp.put("coveraddress", tmp.getCoveraddress());
//			jsTmp.put("discussCount", tmp.getReadcount());
			jsa.add(jsTmp);
		}
		
		if(page!=StaticData.no_param){
			reJsonObject.put(StaticData.reKey.total.getKey(), newsDAO.getNewsCount());
		}
		
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJsonObject.put(StaticData.reKey.newList.getKey(), jsa);
		return reJsonObject;
	}


	@Override
	public JSONObject saveNews(String title, String content,
			String coveraddress, int newstype, String h5url) throws Exception {
		JSONObject reJsonObject = new JSONObject();
		News news = new News();
		news.setTitle(title);
		news.setContent(content);
		news.setCoveraddress(coveraddress);
		news.setNewstype(newstype);
		news.setH5url(h5url);
		news.setCreatetime(TimeUtil.getSysteCurTime());
		newsDAO.createNews(news);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject updateNews(int newid,String title, String content,
			String coveraddress, int newstype, String h5url) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		News news = newsDAO.getNewsByID(newid);
		if(news==null){
			reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJsonObject;
		}
		if(title!=null&&!title.isEmpty()){
			news.setTitle(title);
		}
		
		if(content!=null&&!content.isEmpty()){
			news.setContent(content);
		}
		
		
		if(coveraddress!=null&&!coveraddress.isEmpty()){
			news.setCoveraddress(coveraddress);
		}
		
		if(newstype!=StaticData.no_param){
			news.setNewstype(newstype);
		}
		
		if(h5url!=null&&!h5url.isEmpty()){
			news.setH5url(h5url);
		}
		
		newsDAO.updateNews(news);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject saveNewsType(String typename, String typedesc,
			String logoaddress, int parentid) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		NewsType newsType = new NewsType();
		newsType.setTypename(typename);
		newsType.setTypedesc(typedesc);
		newsType.setLogoaddress(logoaddress);
		newsType.setParentid(parentid);
		newsTypeDAO.createNewsType(newsType);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}

	@Override
	public JSONObject updateNewsType(int typeid, String typename,
			String typedesc, String logoaddress, int parentid)
			throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		NewsType newsType = newsTypeDAO.getNewsTypeByID(typeid);
		if(newsType==null){
			reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJsonObject;
		}
		if(typename!=null&&!typename.isEmpty()){
			newsType.setTypename(typename);
		}
		
		if(typedesc!=null&&!typedesc.isEmpty()){
			newsType.setTypedesc(typedesc);
		}
		
		if(logoaddress!=null&&!logoaddress.isEmpty()){
			newsType.setLogoaddress(logoaddress);
		}
		
		if(parentid!=StaticData.no_param){
			newsType.setParentid(parentid);
		}
		
		newsTypeDAO.updateNewsType(newsType);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject deleteNewsType(int typeid) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		NewsType newsType = newsTypeDAO.getNewsTypeByID(typeid);
		if(newsType==null){
			reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJsonObject;
		}
		newsTypeDAO.deleteNewsTypeByID(typeid);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	private JSONObject getNewsTypePage(NewsType newsType){
		JSONObject reJsonObject =  new JSONObject();
		reJsonObject.put("typeId", newsType.getTypeid());
		reJsonObject.put("typeName", newsType.getTypename());
		return reJsonObject;
	}
	
	@Override
	public JSONObject queryNewsTypeList(int typeid) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		JSONArray jsa = new JSONArray();
		if(typeid==StaticData.no_param){
			List<NewsType> list = newsTypeDAO.getNewsTypeList();
			for(NewsType tmp:list){
				jsa.add(getNewsTypePage(tmp));
			}
		}else{
			NewsType newsType = newsTypeDAO.getNewsTypeByID(typeid);
			if(newsType==null){
				reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				return reJsonObject;
			}
			jsa.add(getNewsTypePage(newsType));
		}
		reJsonObject.put("newsTypeList", jsa);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject saveNewsDiscussRela(String comment, int userid,
			 int parentid, int newid) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		NewsDiscussRela newsD = new NewsDiscussRela();
		newsD.setComment(comment);
		newsD.setUserid(userid);
		newsD.setNewid(newid);
		newsDiscussRelaDAO.createNewsDiscussRela(newsD);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject deleteNewsDiscussRela(int discussid) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		newsDiscussRelaDAO.deleteNewsDiscussRelaByID(discussid);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject queryNewsDiscussRelaList(int discussid, int userid,
			int parentid, int newid,int page) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		List<NewsDiscussRela> list = newsDiscussRelaDAO.getNewsDiscussRelaList(newid, parentid, page, 
				StaticData.no_param, StaticData.no_param, userid);
		JSONArray jsa = (JSONArray) JSONArray.toJSON(list);
//		for(NewsDiscussRela tmp:list){
//			
//		}
		reJsonObject.put("newsDiscussRelaList", jsa);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject saveBanner(String bannername, String coveraddress,
			String h5url, int newid, int matchid, int position,int bannertype)
			throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		Banner banner = new Banner();
		banner.setBannername(bannername);
		banner.setBannertype(bannertype);
		banner.setCoveraddress(coveraddress);
		banner.setCreatetime(TimeUtil.getSysteCurTime());
		banner.setH5url(h5url);
		banner.setMatchid(matchid);
		banner.setNewid(newid);
		banner.setPosition(position);
		bannerDAO.createBanner(banner);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject updateBanner(int bannerId, String bannername,
			String coveraddress, String h5url, int newid, int matchid,
			int position, int bannertype) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		Banner banner = bannerDAO.getBannerByID(bannerId);
		if(banner==null){
			reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			return reJsonObject;
		}
		if(bannername!=null&&!bannername.isEmpty()){
			banner.setBannername(bannername);
		}
		
		if(coveraddress!=null&&!coveraddress.isEmpty()){
			banner.setCoveraddress(coveraddress);
		}
		
		if(h5url!=null&&!h5url.isEmpty()){
			banner.setH5url(h5url);
		}
		
		if(bannertype!=StaticData.no_param){
			banner.setBannertype(bannertype);
		}
		
		if(matchid!=StaticData.no_param){
			banner.setMatchid(matchid);
		}
		
		if(newid!=StaticData.no_param){
			banner.setNewid(newid);
		}
		
		if(position!=StaticData.no_param){
			banner.setPosition(position);
		}
		
		bannerDAO.updateBanner(banner);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject deleteBanner(int bannerId) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		bannerDAO.deleteBannerByID(bannerId);
		reJsonObject.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJsonObject;
	}


	@Override
	public JSONObject queryBannerList(int page, int bannerId) throws Exception {
		JSONObject reJsonObject =  new JSONObject();
		List<Banner> list = null;
		if(bannerId==StaticData.no_param){
			list = bannerDAO.getBannerList(page);
		}else{
			list = new ArrayList<Banner>();
			Banner tmp = bannerDAO.getBannerByID(bannerId);
			if(tmp!=null){
				list.add(tmp);
			}
		}
		
		JSONArray jsa = new JSONArray();
		JSONObject jsTmp = null;
		for(Banner tmp:list){
			jsTmp = new JSONObject();
			jsTmp.put("bannerid", tmp.getBannerid());
			jsTmp.put("bannername", tmp.getBannername());
			jsTmp.put("bannertype", tmp.getBannertype());
			jsTmp.put("coveraddress", tmp.getCoveraddress());
			jsTmp.put("createtime", TimeUtil.getDateFormat(tmp.getCreatetime()));
			jsTmp.put("h5url", tmp.getH5url());
			jsTmp.put("matchid", tmp.getMatchid());
			jsTmp.put("newid", tmp.getNewid());
			jsTmp.put("position", tmp.getPosition());
			jsa.add(jsTmp);
		}
		reJsonObject.put("bannerList", jsa);
		return reJsonObject;
	}


}
